package com.tmy.controller;


import com.tmy.Dto.Result;
import com.tmy.entity.Dict;
import com.tmy.entity.DictItem;
import com.tmy.service.DictService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 字典管理接口
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;


    /**
     * 新增字典分类
     */
    @PostMapping("/addDict")
    public Result postAddDict(@RequestBody Dict dict){
        Result result = new Result();
        try {
            return dictService.addDict(dict);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 删除字典分类
     */
    @PostMapping("/deleteDict")
    public Result postDeleteDict(@RequestBody Dict dict){
        Result result = new Result();
        try {
            return dictService.deleteDict(dict);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 更新字典分类
     */
    @PostMapping("/updateDict")
    public Result postUpdateDict(@RequestBody Dict dict){
        Result result = new Result();
        try {
            return dictService.updateDict(dict);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 查询字典分类
     */
    @GetMapping("/queryDict")
    public Result getQueryDict( ){
        Result result = new Result();
        try {
            return dictService.queryDict();
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 新增字典值
     */
    @PostMapping("/addDictItem")
    public Result postAddDictItem(@RequestBody DictItem dictItem){
        Result result = new Result();
        try {
            return dictService.addDictItem(dictItem);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 删除字典值
     */
    @PostMapping("/deleteDictItem")
    public Result postDeleteDictItem(@RequestBody DictItem dictItem){
        Result result = new Result();
        try {
            return dictService.deleteDictItem(dictItem);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 更新字典值
     */
    @PostMapping("/updateDictItem")
    public Result postUpdateDictItem(@RequestBody DictItem dictItem){
        Result result = new Result();
        try {
            return dictService.updateDictItem(dictItem);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }

    /**
     * 查询字典值
     */
    @PostMapping("/queryDictItem")
    public Result postQueryDictItem(@RequestBody Dict dict){
        Result result = new Result();
        try {
            return dictService.queryDictItem(dict);
        }catch (Exception err){
            result.setMsg(err.getCause());
            result.setCode(500);
        }
        return result;
    }
}
